package com.wlstock.funi.dao;

import java.sql.SQLException;
import java.util.concurrent.Callable;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.misc.TransactionManager;
import com.j256.ormlite.stmt.QueryBuilder;
import com.wlstock.funi.model.BaseStockInfo;

public class StockBaseInfoDao {
	private DataBaseHelper dbHelper;
	
	private static StockBaseInfoDao daoInstance;
	
	private Dao<BaseStockInfo, Integer> baseInfoDao;
	
	@SuppressWarnings("unchecked")
	private StockBaseInfoDao(Context mContext){
		try {
			dbHelper = DataBaseHelper.getHelper(mContext);
			baseInfoDao = dbHelper.getDao(BaseStockInfo.class);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static <T> StockBaseInfoDao getInstance(Context mContext){
		if (daoInstance == null) {
			synchronized (BaseStockInfo.class) {
				if (daoInstance == null) {
					daoInstance = new StockBaseInfoDao(mContext);
				}
			}
		}
		return daoInstance;
	}
	
	public boolean addOrUpdate(BaseStockInfo stkBaseInfo){
		try {
			baseInfoDao.createOrUpdate(stkBaseInfo);
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 批量插入证券基本代码信息数据到本地数据库
	 * @param arr 读取文件后的jsonarr
	 * @return
	 */
	public boolean addWithStkBaseInfoJSList(final JSONArray arr){
		//x这里有点卡，后面处理
		if (arr!=null && arr.length() == queryAllCount()) {
			return true;
		}
		try {
			TransactionManager.callInTransaction(dbHelper.getConnectionSource(), new Callable<Void>() {

				@Override
				public Void call() throws Exception {
					// TODO 批量加入证券基本代码信息
					for (int i = 0; i < arr.length(); i++) {
						JSONObject stk = arr.getJSONObject(i);
						BaseStockInfo infoModel = new BaseStockInfo(stk);
						addOrUpdate(infoModel);
					}
					return null;
				}
			});
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 根据代码查询名字
	 * @param stockNo
	 * @return
	 */
	public String queryName(String stockNo){
		try {
			QueryBuilder<BaseStockInfo, Integer> qb = baseInfoDao.queryBuilder();
			qb.where().like("stockNo", "%"+stockNo+"%");
			BaseStockInfo info = qb.queryForFirst();
			if (info!=null) {
				return info.getStockName();	
			}else {
				return "";
			}
			
//			GenericRawResults<String[]> rawResult = baseInfoDao.queryRaw(qb.prepareStatementString());
			
			
//			GenericRawResults<String[]> rawResult = baseInfoDao.queryRaw("select stockName from BaseStockInfo where stockNo like '\\% ? \\%' ", new String[]{stockNo});
//			if (rawResult != null && rawResult.getResults().size() > 0) {
//				return rawResult.getResults().get(0).toString();	
//			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 查询基本代码表中所有的数据是否为2089
	 * @return 基本代码表中代码数量
	 */
	public int queryAllCount() {
		// TODO 查询基本代码表中所有的数据是否为2089
		try {
			return baseInfoDao.queryForAll().size();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
}
